﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;
using School.Entities;
using School.Common;
using System.Data.Entity;
using School.Admission;



namespace School.Pages.Admission
{
    public partial class Adm_Test : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            var x = (IQueryable<int>)Session["privilage"];

            if (x.Contains(1005) == true)
            {
                if ((IQueryable<int>)Session["privilage"] != null)
                {
                    if (!(Page.IsPostBack))
                    {
                        //security

                        //fill crls
                        Fillcrls();
                        InitializeControls();
                        GridDataBind();
                        ddlSearchCertificate_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
                        ddlSearchGrade_ID.Items.Insert(0, new ListItem("اختيار", "-1"));
                        ddlSearchStage_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
                        ddlCertificate_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
                        ddlGrade_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
                        ddlStage_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

                    }
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }
        # region "Collect Data"

        //***************** This is function to bind data grid  ***************************
        //-**************************************************************************
        protected void GridDataBind()
        {
            Int32 mycount;
            Hashtable SearchTable = new Hashtable();
            if (ddlSearchcampus_Id.SelectedIndex > 0)
            {
                SearchTable.Add("Campus_Id", ddlSearchcampus_Id.SelectedValue.ToString());
            }
            if (ddlSearchCertificate_Id.SelectedIndex > 0)
            {
                SearchTable.Add("Certificate_Id", ddlSearchCertificate_Id.SelectedValue.ToString());
            }
            if (ddlSearchStage_Id.SelectedIndex > 0)
            {
                SearchTable.Add("Stage_Id", ddlSearchStage_Id.SelectedValue.ToString());
            }
            if (ddlSearchGrade_ID.SelectedIndex > 0)
            {
                SearchTable.Add("Grade_ID", ddlSearchGrade_ID.SelectedValue.ToString());
            }
            griddata.DataSource = ADMTest.All(SearchTable);

            griddata.DataBind();

            griddata.Visible = true;
            mycount = griddata.Rows.Count;
            lblRecordNo.Text = "عدد نتائج البحث = " + mycount.ToString();
            if (mycount == 0)
            {
                lblMessage.Text = "لا يوجد بيانات";
            }
            else
            {
                lblMessage.Text = "";
            }

        }

        # endregion


        # region "Fill all crls"

        //***************** This is function to fill crl  ***************************
        //-**************************************************************************

        protected void Fillcrls()
        {
            ddlCertificate_Id.Items.Clear();
            ddlCertificate_Id.Enabled = false;
            ddlStage_Id.Items.Clear();
            ddlStage_Id.Enabled = false;
            ddlGrade_Id.Items.Clear();
            ddlGrade_Id.Enabled = false; 

            ddlcampus_Id.DataSource = Structure.GetCampuses();
            ddlcampus_Id.DataTextField = "Name";
            ddlcampus_Id.DataValueField = "Id";
            ddlcampus_Id.DataBind();
            ddlcampus_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

            

            ddlSearchcampus_Id.DataSource = Structure.GetCampuses();
            ddlSearchcampus_Id.DataTextField = "Name";
            ddlSearchcampus_Id.DataValueField = "Id";
            ddlSearchcampus_Id.DataBind();
            ddlSearchcampus_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

            ddlEductionalYear_Id.DataSource = Structure.GetCurrentEductionalYear();
            ddlEductionalYear_Id.DataTextField = "Name";
            ddlEductionalYear_Id.DataValueField = "id";
            ddlEductionalYear_Id.DataBind();

            IEnumerable<ORG_EductionalYear> CurrenTEDyear = Structure.GetCurrentEductionalYear();
            ddlSearchEductionalYear_Id.DataSource = CurrenTEDyear;
            ddlSearchEductionalYear_Id.DataTextField = "Name";
            ddlSearchEductionalYear_Id.DataValueField = "id";
            ddlSearchEductionalYear_Id.DataBind();
            ddlSearchEductionalYear_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
            string minDate = "1-1-" + CurrenTEDyear.First().StartDate.Year.ToString();
            string MaxDate = "31-12-" + CurrenTEDyear.First().StartDate.Year.ToString();
            //RangeValidator1.MinimumValue = minDate;
            //RangeValidator1.MaximumValue = MaxDate;



        }


        //***************** This is function to initlaize crl  ***************************
        //-**************************************************************************
        protected void InitializeControls()
        {
            lblMessage.Text = "";
            lblMessageAdd.Text = "";
            txtFromDate.Text = "";
            txtToDate.Text = "";
            Fillcrls();
        }

        # endregion


        # region "Mode Handlers"

        //***************** This is function to handle page in add mode  ***************************
        //-**************************************************************************
        protected void PutFormInAddMode()
        {
           
        }


        //***************** This is function to handle page in brows mode  ***************************
        //-**************************************************************************

        protected void PutFormInBrwosMode()
        {
            pnlAdd.Visible = false;
            pnlbrows.Visible = false;

        }

        #endregion


        # region "Grid Events ***********"

        //***************** This is function to handle page when Page Index Changing   ***************************
        //-**************************************************************************

        protected void griddata_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            griddata.PageIndex = e.NewPageIndex;
            GridDataBind();
        }


        //***************** This is function to handle page when Row Deleting  ***************************
        //-**************************************************************************

        protected void griddata_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                griddata.SelectedIndex = e.RowIndex;

                long CID = Int32.Parse(griddata.SelectedDataKey.Value.ToString());
                Boolean successadd = ADMTest.Delete(CID);
                if (successadd == true)
                {
                    GridDataBind();
                    lblMessage.Visible = true;
                    lblMessage.Text = "تم حذف البيانات بنجاح";

                }
                else
                {
                    lblMessage.Visible = true;
                    lblMessage.Text = "لم يتم حذف البيانات بنجاح رجاء حذف المدرسين و المواد المرتبطة بهذا الاختبار";
                }
            }
            catch (Exception)
            {
                lblMessage.Text = " لم يتم حذف البيانات بنجاح رجاء مراجعة مدير النظام";

            }

        }


        //***************** This is function to handle page when Row Data Bound  ***************************
        //-**************************************************************************
        protected void griddata_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton LB = (LinkButton)e.Row.FindControl("LinkButton1");
                LB.Attributes.Add("onclick", "javascript:return " +
                "confirm('هل ترغب في حذف هذا الاختبار ؟؟  ')");

            }
        }

        protected void griddata_SelectedIndexChanged(object sender, EventArgs e)
        {
            pnlAdd.Visible = false;
            pnlbrows.Visible = false;
            pnldetails.Visible = true;
            DetailsView1bind();
        }

        protected void DetailsView1bind()
        {
            Hashtable SearchTable = new Hashtable();

                SearchTable.Add("Id", griddata.SelectedDataKey.Value.ToString());

            
            DetViewdata.DataSource = ADMTest.All(SearchTable);
            DetViewdata.DataBind();
        }

        # endregion
        # region   "Execute commands sent to the form"

        //***************** This is function to handle page when Puser click in الغاء btn  ***************************
        //-**************************************************************************
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            txtFromDate.Text = "";
            txtToDate.Text = "";
            Fillcrls(); 
        }


        //***************** This is function to handle page when Puser click in اضافة btn  ***************************
        //-**************************************************************************

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            InitializeControls();
            lblMessageAdd.Text = "";
            lblMessage.Text = "";
            pnlAdd.Visible = true;
            pnlbrows.Visible = false;
            pnldetails.Visible = false;
        }

        //***************** This is function to handle page when Puser click in انهاء btn  ***************************
        //-**************************************************************************


        protected void btnend_Click(object sender, EventArgs e)
        {
            lblMessageAdd.Text = "";
            lblMessage.Text = "";
            pnlAdd.Visible = false;
            pnlbrows.Visible = true;
            GridDataBind();
        }
        //***************** This is function to handle page when Puser click in حفظ btn  ***************************
        //-**************************************************************************

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ddlGrade_Id.SelectedIndex == 0)
            {
                lblMessageAdd.Text = "رجاء اختيار الصف ";
                return;
            }
            IEnumerable<ORG_EductionalYear> CurrenTEDyear = Structure.GetCurrentEductionalYear();
            if (Convert.ToDateTime(txtToDate.Text).Year != CurrenTEDyear.First().StartDate.Year || Convert.ToDateTime(txtFromDate.Text).Year != CurrenTEDyear.First().StartDate.Year)
            {
                lblMessageAdd.Text = "التاريخ خارج السنه الدراسية ";
                return;
            }
            if (Convert.ToDateTime(txtToDate.Text) < Convert.ToDateTime(txtFromDate.Text))
            {
                lblMessageAdd.Text = "رجاء اختيار التاريخ الثاني اكبر من التاريخ الاول";
                return;
            }

            
            Int64 campus = Convert.ToInt64(ddlcampus_Id.SelectedValue);
            Int64 certifcate = Convert.ToInt64(ddlCertificate_Id.SelectedValue );
            Int64 Stage = Convert.ToInt64(ddlStage_Id.SelectedValue );
            Int64 grade = Convert.ToInt64(ddlGrade_Id.SelectedValue );
            DateTime FromDate=Convert.ToDateTime(txtFromDate.Text);
            DateTime Todate = Convert.ToDateTime(txtToDate.Text);
            Int64 EductionalYear = Convert.ToInt64(ddlEductionalYear_Id.SelectedValue);


            Hashtable SearchTable = new Hashtable();
            if (ddlSearchcampus_Id.SelectedIndex > 0)
            {
                SearchTable.Add("Campus_Id", ddlcampus_Id.SelectedValue.ToString());
            }
            if (ddlSearchCertificate_Id.SelectedIndex > 0)
            {
                SearchTable.Add("Certificate_Id", ddlCertificate_Id.SelectedValue.ToString());
            }
            if (ddlSearchStage_Id.SelectedIndex > 0)
            {
                SearchTable.Add("Stage_Id", ddlStage_Id.SelectedValue.ToString());
            }
            if (ddlSearchGrade_ID.SelectedIndex > 0)
            {
                SearchTable.Add("Grade_ID", ddlStage_Id.SelectedValue.ToString());
            }

            ADM_Test ObjTest = new ADM_Test() { Campus_Id = campus, Certificate_Id = certifcate, Stage_Id = Stage, Grade_Id = grade, FromDate = FromDate, ToDate = Todate, EductionalYear_Id = EductionalYear };
            if (ADMTest.CheckPrevTests(ObjTest)== false)
            {
                lblMessageAdd.Text = "يوجد اختبار سابق لنفس هذا الصف في نفس الفترة";
                return;

            }
            Int64 xx = ADMTest.Add  (ObjTest);

            if (xx != -1)
            {
                lblMessageAdd.Text = "تم الحفظ بنجاح";
                txtFromDate.Text = "";
                txtToDate.Text = "";
            }
            else
            {
                lblMessageAdd.Text = "لم يتم حفظ البيانات";
            }

        }


        # endregion



        # region " Search *********************"




        //***************** This is function to search by  Get All btn ***************************
        //-**************************************************************************
        protected void btnGetAll_Click(object sender, EventArgs e)
        {
            Int32 mycount;
            Hashtable SearchTable = new Hashtable();

            griddata.DataSource = ADMTest.All(SearchTable);

            griddata.DataBind();

            griddata.Visible = true;
            mycount = griddata.Rows.Count;
            lblRecordNo.Text = "عدد نتائج البحث = " + mycount.ToString();
            if (mycount == 0)
            {
                lblMessage.Text = "لا يوجد بيانات";
            }
            else
            {
                lblMessage.Text = "";
            }
        }


        
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            GridDataBind();
        }

        # endregion

        protected void ddlCertificate_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCertificate_Id.SelectedIndex > 0)
            {
                ddlStage_Id.DataSource = Structure.GetStagesByCertificate(Convert.ToInt64(ddlCertificate_Id.SelectedValue));
                ddlStage_Id.DataTextField = "Name";
                ddlStage_Id.DataValueField = "id";
                ddlStage_Id.DataBind();
                ddlStage_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
                ddlStage_Id.Enabled = true;
                ddlGrade_Id.Enabled = false;
            }
         
        }

        protected void ddlStage_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlStage_Id.SelectedIndex > 0)
            {

                ddlGrade_Id.DataSource = Structure.GetGradesByStage(Convert.ToInt64(ddlStage_Id.SelectedValue));
                ddlGrade_Id.DataTextField = "Name";
                ddlGrade_Id.DataValueField = "id";
                ddlGrade_Id.DataBind();
                ddlGrade_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
                ddlGrade_Id.Enabled = true;
            }
        }

        protected void ddlGrade_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void ddlcampus_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlcampus_Id.SelectedIndex > 0)
            {
                ddlCertificate_Id.DataSource = Structure.GetCertificatesByCampus(Convert.ToInt64(ddlcampus_Id.SelectedValue));
                ddlCertificate_Id.DataTextField = "Name";
                ddlCertificate_Id.DataValueField = "Id";
                ddlCertificate_Id.DataBind();
                ddlCertificate_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
                ddlCertificate_Id.Enabled = true;
                ddlGrade_Id.Enabled = false;
                ddlStage_Id.Enabled = false;
            }
            else
            {
                ddlCertificate_Id.Items.Clear();
                ddlCertificate_Id.Enabled = false;
                ddlStage_Id.Items.Clear();
                ddlStage_Id.Enabled = false;
                ddlGrade_Id.Items.Clear();
                ddlGrade_Id.Enabled = false;
            }
        }

        protected void txtShowCampus_TextChanged(object sender, EventArgs e)
        {

        }

        protected void ddlSearchcampus_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlSearchCertificate_Id.DataSource = Structure.GetCertificatesByCampus(Convert.ToInt64(ddlSearchcampus_Id.SelectedValue));
            ddlSearchCertificate_Id.DataTextField = "Name";
            ddlSearchCertificate_Id.DataValueField = "Id";
            ddlSearchCertificate_Id.DataBind();
            ddlSearchCertificate_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
        }

        protected void btnback_Click(object sender, EventArgs e)
        {
            
        }

        protected void ddlSearchCertificate_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlSearchStage_Id.DataSource = Structure.GetStagesByCertificate(Convert.ToInt64(ddlSearchCertificate_Id.SelectedValue));
            ddlSearchStage_Id.DataTextField = "Name";
            ddlSearchStage_Id.DataValueField = "id";
            ddlSearchStage_Id.DataBind();
            ddlSearchStage_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
        }

        protected void ddlSearchStage_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlSearchGrade_ID.DataSource = Structure.GetGradesByStage(Convert.ToInt64(ddlSearchStage_Id.SelectedValue));
            ddlSearchGrade_ID.DataTextField = "Name";
            ddlSearchGrade_ID.DataValueField = "id";
            ddlSearchGrade_ID.DataBind();
            ddlSearchGrade_ID.Items.Insert(0, new ListItem("اختيار", "-1"));
        }

    }
}